/*
 * Copyright (c) 2024, Qi Yaolong.
 * dragon@hbu.edu.cn
 * HeBei University, China.
 * 
 * 简体中文
 * 保留所有权利。除非符合（遵守）以下条款，否则您不得使用此文件：
 * 
 * 禁止任何未经授权的个人或组织以任何形式将本程序及其附属资料、创新、创意、架构设计、算法、衍生作品
 * 等用于任何商业目的、盈利活动、各类竞赛(比赛)、直播教学、录播教学、线下课程、书籍编写、教材编写、
 * 会议、培训、公益活动、项目课题、毕业设计、毕业论文、学术论文等。
 * 
 * 本程序由版权持有人和贡献人“按原样”提供，不提供任何明示或暗示担保，包括但不局限于对适销性和特定
 * 用途适合性的暗示担保。在任何情况下，版权持有人或贡献人对因使用本程序而导致的任何直接、间接、附带、
 * 特殊、典型或后果性损害（包括但不限于购买替代商品或服务；使用损失、数据丢失或利润损失；业务中断）
 * 不承担任何责任，无论其如何产生，也不论其责任理论为何，无论是合同、严格责任还是侵权（包括疏忽或
 * 其他），即使已告知此类损害的可能性。
 * 
 * English
 * All rights reserved. You may NOT use this file except in compliance with the
 * following terms:
 * 
 * IT IS PROHIBITED FOR ANY UNAUTHORIZED PERSON OR ENTITY TO USE THIS PROGRAM AND 
 * ITS ANCILLARY DATA, INNOVATIONS, CONCEPTS, ARCHITECTURE DESIGN, ALGORITHMS, 
 * DERIVED WORKS, ETC., IN ANY FORM FOR COMMERCIAL PURPOSES, PROFIT-MAKING 
 * ACTIVITIES, VARIOUS COMPETITIONS (CONTESTS), LIVE TEACHING, PRE-RECORDED 
 * LESSONS, OFFLINE COURSES, BOOK WRITING, CONFERENCES, TRAININGS, PUBLIC WELFARE 
 * ACTIVITIES, PROJECTS, TOPICS, GRADUATION DESIGNS, DISSERTATIONS, THESES, AND 
 * OTHER APPLICATIONS.
 * 
 * THIS PROGRAM IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS PROGRAM, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <stdio.h>        
#include <unistd.h>        
#include "ohos_init.h"     
#include "cmsis_os2.h"     
#include "iot_gpio.h"      
#include "../inc/iot_gpio_ex.h"
#include "../inc/oled_ssd1306.h"   
#include "../inc/mpu6050.h"       

MPU6050_Data_t sensor_data;

uint8_t fonts1[][32] = {
    {0x04, 0x04, 0x04, 0x04, 0x04, 0xFC, 0x44, 0x44, 0x44, 0x44, 0x44, 0xC4, 0x04, 0x04, 0x04, 0x00, 0x80, 0x40, 0x20, 0x18, 0x06, 0x01, 0x00, 0x00, 0x40, 0x80, 0x40, 0x3F, 0x00, 0x00, 0x00, 0x00}, /*"万",0*/
    {0x40, 0x3C, 0x10, 0xFF, 0x10, 0x10, 0x20, 0x10, 0x8F, 0x78, 0x08, 0xF8, 0x08, 0xF8, 0x00, 0x00, 0x02, 0x06, 0x02, 0xFF, 0x01, 0x01, 0x04, 0x42, 0x21, 0x18, 0x46, 0x81, 0x40, 0x3F, 0x00, 0x00}, /*"物",1*/
    {0x00, 0x02, 0x02, 0x02, 0xE2, 0x3E, 0x22, 0x22, 0x22, 0x22, 0x22, 0xE2, 0x02, 0x02, 0x00, 0x00, 0x40, 0x40, 0x40, 0x47, 0x44, 0x44, 0x44, 0x44, 0x44, 0x74, 0x4E, 0x41, 0x40, 0x40, 0x40, 0x00}, /*"互",2*/
    {0x02, 0xFE, 0x92, 0x92, 0xFE, 0x02, 0x00, 0x10, 0x11, 0x16, 0xF0, 0x14, 0x13, 0x10, 0x00, 0x00, 0x10, 0x1F, 0x08, 0x08, 0xFF, 0x04, 0x81, 0x41, 0x31, 0x0D, 0x03, 0x0D, 0x31, 0x41, 0x81, 0x00}, /*"联",3*/
};

uint8_t fonts2[] = {

    //*-- ID:0,字符:"速",ASCII编码:CBD9,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x40,0x42,0x44,0xcc,0x00,0xf4,0x94,0x94,0x94,0xff,0x94,0x94,0x94,0xf6,0x04,0x00,
    0x00,0x40,0x20,0x1f,0x20,0x51,0x48,0x44,0x42,0x7f,0x42,0x44,0x4c,0x61,0x20,0x00,

    /*-- ID:1,字符:"度",ASCII编码:B6C8,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0x00,0xfc,0x24,0x24,0x24,0xfc,0xa5,0xa6,0xa4,0xfc,0x24,0x34,0x26,0x04,0x00,
    0x40,0x20,0x9f,0x80,0x42,0x42,0x26,0x2a,0x12,0x2a,0x26,0x42,0x40,0xc0,0x40,0x00,

    /*-- ID:2,字符:"加",ASCII编码:BCD3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x10,0x10,0x10,0xff,0x10,0x10,0xf8,0x10,0x00,0xf8,0x08,0x08,0x08,0xfc,0x08,0x00,
    0x40,0x20,0x1e,0x01,0x20,0x40,0x3f,0x00,0x00,0x7f,0x20,0x20,0x20,0x7f,0x00,0x00,

    /*-- ID:3,字符:"危",ASCII编码:CEA3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0x10,0x08,0xf4,0x13,0x12,0x92,0x92,0x9a,0x96,0x92,0xd0,0x98,0x10,0x00,0x00,
    0x80,0x40,0x30,0x0f,0x00,0x00,0x3f,0x40,0x40,0x44,0x48,0x47,0x40,0x40,0x70,0x00,

    /*-- ID:4,字符:"险",ASCII编码:CFD5,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0xfe,0x22,0x5a,0x86,0x20,0x90,0x28,0x24,0xa3,0x34,0x28,0x90,0x30,0x10,0x00,
    0x00,0xff,0x04,0x08,0x47,0x40,0x41,0x5e,0x40,0x5f,0x60,0x58,0x47,0x60,0x40,0x00,

    /*-- ID:5,字符:"：",ASCII编码:A3BA,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x00,0x00,0x00,0x00,0x80,0xc0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x31,0x7b,0x7b,0x31,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    /*-- ID:6,字符:"安",ASCII编码:B0B2,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x90,0x8c,0x84,0x84,0x84,0x84,0xf5,0x86,0x84,0x84,0x84,0x84,0x84,0xd4,0x8c,0x00,
    0x00,0x00,0x80,0x84,0x46,0x49,0x28,0x10,0x10,0x28,0x47,0xc0,0x00,0x00,0x00,0x00,

    /*-- ID:7,字符:"全",ASCII编码:C8AB,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
    0x80,0x80,0x40,0x20,0x50,0x48,0x44,0xc3,0x44,0x68,0x50,0x20,0x40,0xc0,0x40,0x00,
    0x00,0x40,0x40,0x40,0x44,0x44,0x44,0x7f,0x44,0x46,0x44,0x40,0x60,0x40,0x00,0x00,

    };

void *mpu6050_task( void )
{
    while (1)
    {
        MPU6050_GetData(&sensor_data);

        osal_printk("Accel: X=%d Y=%d Z=%d\n"
               "Gyro:  X=%d Y=%d Z=%d\n",
               sensor_data.accel.x,
               sensor_data.accel.y,
               sensor_data.accel.z,
               sensor_data.gyro.x,
               sensor_data.gyro.y,
               sensor_data.gyro.z);

        msleep(500); 
    }
    return NULL;
}


void OledShowChinese1(uint8_t x, uint8_t y, uint8_t idx)
{

    uint8_t t;

    OledSetPosition(x, y);
    for (t = 0; t < 16; t++)
    {
        WriteData(fonts1[idx][t]);
    }

    OledSetPosition(x, y + 1);
    for (t = 16; t < 32; t++)
    {
        WriteData(fonts1[idx][t]);
    }
}


void OledShowChinese2(uint8_t x, uint8_t y, uint8_t idx)
{

    uint8_t t;

    OledSetPosition(x, y);
    for (t = 0; t < 16; t++)
    {
        WriteData(fonts2[32 * idx + t]);
    }

    OledSetPosition(x, y + 1);
    for (t = 16; t < 32; t++)
    {
        WriteData(fonts2[32 * idx + t]);
    }
}

void OledShowNumber(uint8_t x, uint8_t y, int num)
{
    char buffer[5];
    sprintf(buffer, "%d", num);
    OledSetPosition(x, y);
    for (int i = 0; buffer[i] != '\0'; i++)
    {
        uint8_t idx = buffer[i] - '0';

        for (uint8_t t = 0; t < 16; t++)
        {
            WriteData(fonts2[32 * idx + t]);
        }

        OledSetPosition(x + 8, y + 8); 
        for (uint8_t t = 16; t < 32; t++)
        {
            WriteData(fonts2[32 * idx + t]);
        }
        x += 8; 
    }
}


void OledTask1(void)
{

    OledFillScreen(0x00);
    OledShowChinese2(5 + 0, 0, 0);  
    OledShowChinese2(5 + 18, 0, 1); 
    OledShowChinese2(5 + 36, 0, 5);  
    OledShowChinese2(5 + 0, 2, 2);  
    OledShowChinese2(5 + 18, 2, 0);  
    OledShowChinese2(5 + 36, 2, 1); 
    OledShowChinese2(5 + 54, 2, 5);  
    OledShowChinese2(5 + 0, 4, 6); 
    OledShowChinese2(5 + 18, 4, 7); 
    OledShowNumber(5 + 54, 0, sensor_data.accel.x); 
    OledShowNumber(5 + 72, 2, sensor_data.gyro.x); 
}

void OledTask2(void)
{

    OledFillScreen(0x00);

    OledShowChinese2(5 + 0, 0, 0);
    OledShowChinese2(5 + 18, 0, 1); 
    OledShowChinese2(5 + 36, 0, 5);  
    OledShowChinese2(5 + 0, 2, 2); 
    OledShowChinese2(5 + 18, 2, 0); 
    OledShowChinese2(5 + 36, 2, 1);
    OledShowChinese2(5 + 54, 2, 5);  
    OledShowChinese2(5 + 0, 4, 3); 
    OledShowChinese2(5 + 18, 4, 4);
    OledShowNumber(5 + 54, 0, sensor_data.accel.x); 
    OledShowNumber(5 + 72, 2, sensor_data.gyro.x);  
}

void OledTask(void)
{

    OledFillScreen(0x00);

    OledShowChinese2(5 + 0, 0, 0);  
    OledShowChinese2(5 + 18, 0, 1); 
    OledShowChinese2(5 + 36, 0, 5); 
    OledShowChinese2(5 + 0, 2, 2); 
    OledShowChinese2(5 + 18, 2, 0);  
    OledShowChinese2(5 + 36, 2, 1); 
    OledShowChinese2(5 + 54, 2, 5);  
}

